package com.epfresh.supplier.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.global.MessageCentre;
import com.epfresh.api.utils.T;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.HomeNewUpadteEvenbus;
import com.tencent.mmkv.MMKV;

import org.greenrobot.eventbus.EventBus;

public class FirstPostAuditActivity extends BaseActivity {

    TextView tvBtn;
    private int toIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_post_audit);
        initView();
    }

    private void initView() {
        toIndex = MMKV.defaultMMKV().decodeInt("toIndex", -1);
        tvBack.setVisibility(View.GONE);
        ivBack.setVisibility(View.GONE);
        basetitle.setText("审核中");
        tvBtn = findViewById(R.id.tv_btn);
        tvBtn.setOnClickListener(this);
        if (toIndex==2) {
            tvBtn.setText("返回首页");
        } else {
            tvBack.setVisibility(View.VISIBLE);
            tvBack.setText("上一步");
            tvBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    intoLogin();
                }
            });
        }
    }

    @Override
    protected void onToolbarBack() {
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_btn:
                if (toIndex==2) {
                    intoHome();
                } else {
                    intoLogin();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        tvBtn.performClick();
    }

    public void intoHome() {
        Intent intent = new Intent(this, HomeNewActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        EventBus.getDefault().post(new HomeNewUpadteEvenbus());
        finish();
    }

    private void intoLogin() {
        MMKV.defaultMMKV().encode("token", "");
        MessageCentre.getInstance().logoutAlias();
        SharedPreferences user = mContext.getSharedPreferences(
                "userinfo", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = user.edit();
        editor.putString("password", "");
        editor.putString("accountId", "");
        editor.putString("access_token", "");
        editor.commit();
        clearUser();
        clearStoreInfo();
        BaseApplication.getInstance().putMerchant(null);
        Intent main = new Intent(this, LoginActivity.class);
        main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(main);
        finish();
    }


}
